package com.example.rentingapp.activity;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.example.rentingapp.R;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.enetity.InformationEntity;
import com.example.rentingapp.enetity.InformationResponse;
import com.example.rentingapp.util.StringUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//个人信息界面
public class MyInformationActivity extends BaseActivity implements View.OnClickListener {
    private Button btnModify, btnBack;
    private EditText etName, etPhone, etSex, etAge, etIdCard;
    private String phone, informationUserId, name, sex, age, idCard;
    private List<InformationEntity> datas = new ArrayList<>();

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    etName.setText(datas.get(0).getName());
                    etPhone.setText(String.valueOf(datas.get(0).getPhone()));
                    etSex.setText(datas.get(0).getSex());
                    etAge.setText(String.valueOf(datas.get(0).getAge()));
                    etIdCard.setText(datas.get(0).getId_card());
                    break;
                case 1:
                    etName.setText(datas.get(0).getName());
                    etPhone.setText(String.valueOf(datas.get(0).getPhone()));
                    etSex.setText(datas.get(0).getSex());
                    etAge.setText(String.valueOf(datas.get(0).getAge()));
                    etIdCard.setText(datas.get(0).getId_card());
                    showToast("个人信息完善成功");
                    break;
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.activity_my_information;
    }

    @Override
    protected void initView() {
        btnModify = findViewById(R.id.btn_modify);
        btnBack = findViewById(R.id.btn_back);
        etName = findViewById(R.id.et_name);
        etPhone = findViewById(R.id.et_phone);
        etSex = findViewById(R.id.et_sex);
        etAge = findViewById(R.id.et_age);
        etIdCard = findViewById(R.id.et_id_card);
    }

    @Override
    protected void initData() {
        btnModify.setOnClickListener(this);
        btnBack.setOnClickListener(this);

        informationUserId = findByKey("user_id");
        searchInformation();//搜索用户个人信息
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_modify:
                name = etName.getText().toString().trim();
                sex = etSex.getText().toString().trim();
                age = etAge.getText().toString().trim();
                phone = etPhone.getText().toString().trim();
                idCard = etIdCard.getText().toString().trim();
                publishInformation();
                break;
            case R.id.btn_back:
                finish();
                break;
        }
    }

    private void searchInformation() {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("information_user_id", informationUserId);//从本地获取user_id
        Api.config(ApiConfig.FIND_INFORMATION, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                InformationResponse response = gson.fromJson(res, InformationResponse.class);
                if (response.getSuccess() == true) {
                    List<InformationEntity> list = response.getDatas();
                    if (list != null && list.size() > 0) {
                        datas = list;
                        mHandler.sendEmptyMessage(0);
                    }
                } else {
                    showToastSync("请完善个人信息");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }

    public void publishInformation() {
        if (StringUtils.isEmpty(name)) {
            showToast("请输入姓名");
            return;
        }
        if (StringUtils.isEmpty(sex)) {
            showToast("请输入性别");
            return;
        }
        if (StringUtils.isEmpty(age)) {
            showToast("请输入年龄");
            return;
        }
        if (StringUtils.isEmpty(phone)) {
            showToast("请输入手机号码");
            return;
        }
        if (StringUtils.isEmpty(idCard)) {
            showToast("请输入身份证号码");
            return;
        }

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("information_user_id", informationUserId);
        params.put("name", name);
        params.put("sex", sex);
        params.put("age", age);
        params.put("phone", phone);
        params.put("id_card", idCard);
        Api.config(ApiConfig.POST_INFORMATION, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                InformationResponse response = gson.fromJson(res, InformationResponse.class);
                if (response.getSuccess() == true) {
                    List<InformationEntity> list = response.getDatas();
                    if (list != null && list.size() > 0) {
                        datas = list;
                        mHandler.sendEmptyMessage(1);
                    }
                } else {
                    showToastSync("个人信息完善失败");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }
}